package com.ydzh.kfqgwvisitor.util;

import com.baidu.aip.face.AipFace;
import com.baidu.aip.face.FaceVerifyRequest;
import com.baidu.aip.ocr.AipOcr;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;

/**
 * 身份证识别
 */
@Slf4j
public class Idcard {

    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 百度OCR 身份证识别
     * API Key：4gGVPG0zrCFU1l80ku6S1GUW
     * Secret Key：NE5Dg9q18ff8Q4CzXQYB1cs2IRMtZx6s
     */
    // 身份证识别
    public static JSONObject idCard(byte[] bytes, String side) {
        HashMap<String, String> options = new HashMap<>(6);

        options.put("detect_direction", "true");
        options.put("detect_risk", "true");//是否开启身份证风险类型(身份证复印件、临时身份证、身份证翻拍、修改过的身份证)功能，默认不开启，即：false。可选值:true-开启；false-不开启
        AipOcr aipOcrJava = new AipOcr("24815411", "4gGVPG0zrCFU1l80ku6S1GUW", "NE5Dg9q18ff8Q4CzXQYB1cs2IRMtZx6s");
        // JSONObject jsonObject = aipOcrJava.basicAccurateGeneral(file, options);  或者是传url
        /*byte[] bytes = new byte[0];
        try {
            bytes = Util.readFileByBytes(file);
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        JSONObject jsonObject = aipOcrJava.idcard(bytes, side, options);
        //   ResultBean bean = com.alibaba.fastjson.JSONObject.toJavaObject(com.alibaba.fastjson.JSONObject.parseObject(jsonObject.toString()), ResultBean.class);

        return jsonObject;
    }

    public static void main(String[] args) throws IOException {
       /* String filePath = "C:\\Users\\Administrator\\Pictures\\2.png";
        byte[] imgData = FileUtil.readFileByBytes(filePath);
        JSONObject front = Idcard.idCard(imgData, "front");
        System.out.println(front.toString());*/
       /* IdcardUtil idTest = new IdcardUtil("24815411", "4gGVPG0zrCFU1l80ku6S1GUW", "NE5Dg9q18ff8Q4CzXQYB1cs2IRMtZx6s");
        HashMap<String, String> options = new HashMap<>(2);
        options.put("detect_direction", "true");
        options.put("detect_risk", "true");
        JSONObject jsonObject = idTest.idcard("img", "front", options);
        if (jsonObject.has("error_msg")) {
            System.out.println("照片错误");
        }
        JSONObject words_result = jsonObject.getJSONObject("words_result");
        System.out.println(words_result.getJSONObject("姓名").get("words"));
        System.out.println(words_result.getJSONObject("公民身份号码").get("words"));
        System.out.println(words_result.getJSONObject("性别").get("words"));
        System.out.println(jsonObject);*/
        /*FaceVerifyRequest base64 = new FaceVerifyRequest(URLEncoder.encode(, "UTF-8"), "BASE64");*/
        FaceVerifyRequest base64 = new FaceVerifyRequest("", "BASE64");
        AipFace aipFace = new AipFace("24815411", "mEubNlMYwZ5DeQkhmPE1va4E", "cODp7sEHVYRc3V2ohQlDzY5ngZZA4Ggq");
        JSONObject jsonObject = aipFace.idMatch("320323198902153850", "陈昊", null);
        if (jsonObject.getInt("error_code") == 0) {
            System.out.println("成功");
        } else {
            System.out.println("失败");
        }
        /*List<FaceVerifyRequest> input = new ArrayList<>();
        input.add(base64);
        JSONObject faceverify = aipFace.faceverify(input);
        if (faceverify.getInt("error_code") != 0) {
            System.out.println("认证失败");
        }
        System.out.println(faceverify.getJSONObject("result").getBigDecimal("face_liveness").compareTo(new BigDecimal("0.95")) == -1);*/
        System.out.println(jsonObject);

    }


}
